package com.example.echo;

import io.netty.bootstrap.ServerBootstrap;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelOption;
import io.netty.channel.EventLoopGroup;
import io.netty.channel.nio.NioEventLoopGroup;
import io.netty.channel.socket.SocketChannel;
import io.netty.channel.socket.nio.NioServerSocketChannel;

import java.net.InetSocketAddress;

/**
 * @Description
 * @Author 26233
 * @Create 2021-10-28 22:47:36
 */

public class EchoServer {

    String hostName = "192.168.101.8";
    int port = 8888;

    EventLoopGroup bossGroup;
    EventLoopGroup taskGroup;
    ServerBootstrap server;

    private void init(){
        bossGroup = new NioEventLoopGroup(1);
        taskGroup = new NioEventLoopGroup();

        server = new ServerBootstrap();

        server.group(bossGroup, taskGroup)
                .channel(NioServerSocketChannel.class)
                .option(ChannelOption.SO_BACKLOG, 100)
                .childHandler(new ChannelInitializer<SocketChannel>() {
                    @Override
                    protected void initChannel(SocketChannel socketChannel) throws Exception {

                    }
                });
        try {
            ChannelFuture ch = server.bind(new InetSocketAddress(hostName, port)).sync();
            System.out.println("server start...");
            ch.channel().closeFuture().sync();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }finally {
            bossGroup.shutdownGracefully();
            taskGroup.shutdownGracefully();
        }

    }

    public static void main(String[] args) {
        new EchoServer().init();
    }

}
